/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsc.temporary.memcache;


import java.util.concurrent.TimeoutException;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author mac
 */
@Service
public class MemCacheManager {
   @Autowired
    private MemcachedClientFactoryBean memcachedClient;
   
    private static final Logger log = LoggerFactory.getLogger(MemCacheManager.class);
   
    private MemcachedClient getCilent(){
        return memcachedClient.getObject();
    }
   
   //写入对象
    public void add(String key,int exp,Object obj) {
        try {
            getCilent().add(key, exp, obj);
        } catch (TimeoutException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (InterruptedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (MemcachedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        }
    }
   //根据key获得对象
    public Object get(String key){
       
        try {
             return getCilent().get(key);
        } catch (TimeoutException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (InterruptedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (MemcachedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        }
        return null;
    }
    //删除对象
    public boolean delete(String key){
        try {
            return getCilent().delete(key);
        } catch (TimeoutException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (InterruptedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (MemcachedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        }
        return false;
    }
    //替换对象
    public boolean replace(String key,int exp,Object obj){
        try {
            return getCilent().replace(key, exp, obj);
        } catch (TimeoutException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (InterruptedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (MemcachedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        }
        return false;
    
    }
    //set 对象
    public boolean set(String key,int exp,Object obj){
    
        try {
            return getCilent().set(key, exp, obj);
        } catch (TimeoutException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (InterruptedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        } catch (MemcachedException ex) {
            log.debug(MemCacheManager.class.getName()+ex.toString());
        }
        return false;
    }
}
